package com.carrental.crm.controller;

import com.carrental.common.result.Result;
import com.carrental.crm.dto.CustomerDTO;
import com.carrental.crm.dto.CustomerLoginDTO;
import com.carrental.crm.dto.CustomerRegisterDTO;
import com.carrental.crm.service.CustomerService;
import com.carrental.crm.vo.CustomerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户控制器
 */
@Slf4j
@Api(tags = "客户管理")
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
public class CustomerController {

    private final CustomerService customerService;

    @ApiOperation("发送手机验证码")
    @PostMapping("/sendSmsCode")
    public Result<Boolean> sendSmsCode(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        log.info("发送验证码，手机号：{}", phone);
        return Result.success(customerService.sendSmsCode(phone));
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<Boolean> register(@Valid @RequestBody CustomerRegisterDTO registerDTO) {
        log.info("用户注册，手机号：{}", registerDTO.getPhone());
        return Result.success(customerService.register(registerDTO));
    }

    @ApiOperation("验证码登录")
    @PostMapping("/loginBySms")
    public Result<Map<String, Object>> loginBySms(@Valid @RequestBody CustomerLoginDTO loginDTO) {
        log.info("验证码登录，手机号：{}", loginDTO.getPhone());
        String token = customerService.loginBySms(loginDTO);
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        return Result.success(result);
    }

    @ApiOperation("用户名和密码登录登录")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@Valid @RequestBody CustomerLoginDTO loginDTO) {
        log.info("验证码登录，手机号：{}", loginDTO.getPhone());
        String token = customerService.loginBySms(loginDTO);
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        return Result.success(result);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public Result<CustomerVO> getUserInfo() {
        log.info("获取用户信息");
        return Result.success(customerService.getCurrentCustomerInfo());
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/update")
    public Result<Boolean> updateCustomerInfo(@RequestBody CustomerDTO customerDTO) {
        log.info("更新用户信息：{}", customerDTO);
        boolean result = customerService.updateCustomerInfo(customerDTO);
        return Result.success(result);
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Result<Boolean> logout() {
        log.info("退出登录");
        return Result.success(customerService.logout());
    }

    /**
     * 发送验证码短信
     *
     * @param phoneNumber 手机号码
     * @param code        验证码
     * @return 发送结果
     */
    @Operation(summary = "发送验证码短信", description = "向指定手机号发送验证码短信")
    @GetMapping("/verification-code")
    public Result<Boolean> sendVerificationCode(
            @Parameter(description = "手机号码", required = true) @RequestParam String phoneNumber,
            @Parameter(description = "验证码", required = true) @RequestParam String code) {
        Integer result = customerService.sendVerificationCode(phoneNumber, code);
        return result == 1 ? Result.success(true) : Result.failed("发送验证码短信失败");
    }

    /**
     * 发送通知短信
     *
     * @param phoneNumber    手机号码
     * @param templateParams 模板参数
     * @return 发送结果
     */
    @Operation(summary = "发送通知短信", description = "向指定手机号发送通知短信")
    @GetMapping("/notification")
    public Result<Boolean> sendNotification(
            @Parameter(description = "手机号码", required = true) @RequestParam String phoneNumber,
            @Parameter(description = "模板参数", required = true) @RequestBody Map<String, String> templateParams) {
        boolean result = customerService.sendNotification(phoneNumber, templateParams);
        return result ? Result.success(true) : Result.failed("发送通知短信失败");
    }

    /**
     * 发送营销短信
     *
     * @param phoneNumber    手机号码
     * @param templateParams 模板参数
     * @return 发送结果
     */
    @Operation(summary = "发送营销短信", description = "向指定手机号发送营销短信")
    @GetMapping("/marketing")
    public Result<Boolean> sendMarketing(
            @Parameter(description = "手机号码", required = true) @RequestParam String phoneNumber,
            @Parameter(description = "模板参数", required = true) @RequestBody Map<String, String> templateParams) {
        boolean result = customerService.sendMarketing(phoneNumber, templateParams);
        return result ? Result.success(true) : Result.failed("发送营销短信失败");
    }
}